package com.bjgzy.courselive.mvp.ui.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.bjgzy.courselive.di.component.DaggerOrderPayComponent;
import com.bjgzy.courselive.di.module.OrderPayModule;
import com.bjgzy.courselive.mvp.contract.OrderPayContract;
import com.bjgzy.courselive.mvp.presenter.OrderPayPresenter;
import com.bjgzy.module_couselive.R;
import com.bjgzy.module_couselive.R2;
import com.blankj.utilcode.util.ObjectUtils;
import com.eduhzy.ttw.commonres.utils.RxUtil;
import com.eduhzy.ttw.commonsdk.core.Constants;
import com.eduhzy.ttw.commonsdk.core.EventBusTags;
import com.eduhzy.ttw.commonsdk.core.RouterHub;
import com.eduhzy.ttw.commonsdk.entity.RightOrderInfo;
import com.jess.arms.base.BaseActivity;
import com.jess.arms.di.component.AppComponent;
import com.jess.arms.utils.ArmsUtils;
import com.qmuiteam.qmui.alpha.QMUIAlphaImageButton;
import com.qmuiteam.qmui.widget.QMUIEmptyView;
import com.qmuiteam.qmui.widget.dialog.QMUITipDialog;

import org.simple.eventbus.EventBus;

import butterknife.BindView;
import butterknife.OnClick;

import static com.jess.arms.utils.Preconditions.checkNotNull;

/**
 * @author xubo
 * @date 2018-8-3 13:44:39
 */
@Route(path = RouterHub.COURSELIVE_ORDERPAYACTIVITY)
public class OrderPayActivity extends BaseActivity<OrderPayPresenter> implements OrderPayContract.View {
    @Autowired(name = Constants.ROUTER_INTEGER)
    int internalId = -1;
    @Autowired(name = Constants.ROUTER_DATA)
    RightOrderInfo info;

    @BindView(R2.id.btn_alipay)
    QMUIAlphaImageButton btnAlipay;
    @BindView(R2.id.btn_wechat)
    QMUIAlphaImageButton btnWechat;
    @BindView(R2.id.tv_shop)
    TextView tvShop;
    @BindView(R2.id.tv_title)
    TextView tvTitle;
    @BindView(R2.id.tv_project)
    TextView tvProject;
    @BindView(R2.id.tv_price)
    TextView tvPrice;
    @BindView(R2.id.emptyView)
    QMUIEmptyView emptyView;
    private RightOrderInfo rightOrderInfo;

    @Override
    public void setupActivityComponent(@NonNull AppComponent appComponent) {
        //如找不到该类,请编译一下项目
        DaggerOrderPayComponent
                .builder()
                .appComponent(appComponent)
                .orderPayModule(new OrderPayModule(this))
                .build()
                .inject(this);
    }

    @Override
    public int initView(@Nullable Bundle savedInstanceState) {
        //如果你不需要框架帮你设置 setContentView(id) 需要自行设置,请返回 0
        return R.layout.courselive_activity_order_pay;
    }

    @Override
    public void initData(@Nullable Bundle savedInstanceState) {
        if (ObjectUtils.isNotEmpty(info)) {
            update(info);
        } else if (internalId > 0) {
            mPresenter.saveRightOrder(internalId);
        }
    }

    @Override
    public void showLoading() {
        emptyView.show(true);
    }

    @Override
    public void hideLoading() {

    }


    @Override
    public void showMessage(@NonNull String message) {
        checkNotNull(message);
        RxUtil.showTipDialog(getActivity(), QMUITipDialog.Builder.ICON_TYPE_NOTHING, message);
    }

    @Override
    public void launchActivity(@NonNull Intent intent) {
        checkNotNull(intent);
        ArmsUtils.startActivity(intent);
    }

    @Override
    public void killMyself() {
        finish();
    }

    @Override
    public Activity getActivity() {
        return this;
    }

    @Override
    public void update(RightOrderInfo data) {
        this.rightOrderInfo = data;
        mPresenter.getOrderPayStatus(String.valueOf(data.getOrderNoId()));
        tvTitle.setText(data.getTitle());
        tvShop.setText(data.getShopName());
        tvPrice.setText(ObjectUtils.isEmpty(data.getPayPriceStr()) ? "¥0" : "¥" + data.getPayPriceStr());
        emptyView.hide();
    }

    @Override
    public void paySuccess() {
        Message msg = Message.obtain();
        msg.what = EventBusTags.PAY_SUCCESS;
        EventBus.getDefault().post(msg,RouterHub.COURSELIVE_MINEORDERFRAGMENT);
        EventBus.getDefault().post(msg,RouterHub.COURSELIVE_COURSEPLAYACTIVITY);
        EventBus.getDefault().post(msg,RouterHub.COURSELIVE_COURSEINTRODUCTIONFRAGMENT);
    }

    @OnClick({R2.id.btn_alipay, R2.id.btn_wechat})
    public void onViewClicked(View view) {
        int i = view.getId();
        if (i == R.id.btn_alipay) {
            alipay();
        } else if (i == R.id.btn_wechat) {
            wechatPay();
        }
    }

    private void wechatPay() {
        mPresenter.getWxPayOrderInfo(rightOrderInfo.getItemList().get(0).getProductType(), rightOrderInfo.getTitle(), rightOrderInfo.getPayPrice(), rightOrderInfo.getShopId(), rightOrderInfo.getOrderNoId());
    }

    private void alipay() {
        mPresenter.getAliPayOrderInfo(rightOrderInfo.getItemList().get(0).getProductType(), rightOrderInfo.getTitle(), rightOrderInfo.getPayPrice(), rightOrderInfo.getShopId(), rightOrderInfo.getOrderNoId());
    }

    private int getPayYuan(String money) {
        float fyuan = Float.parseFloat(money);
        int yuan = (int) (fyuan * 100);
        return yuan;
    }

}
